/*
 * Copyright (c) 2009, Luke Emrose
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the
 * distribution.  Neither the name of Luke Emrose Sole Trader nor the
 * names of its contributors may be used to endorse or promote
 * products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
 #ifndef __NOISES_GLOBALS_H__
#define __NOISES_GLOBALS_H__

#define USE_SSE

// platform specifics
#ifdef WIN32
	#define WIN32_LEAN_AND_MEAN

	#pragma warning( push )
	#pragma warning( disable : 4005 )
	#pragma warning( disable : 4265 )
	#pragma warning( disable : 4263 )
	#pragma warning( disable : 4264 )
	#pragma warning( disable : 4625 )
	#pragma warning( disable : 4626 )
	#pragma warning( disable : 4917 )
	#pragma warning( disable : 4244 )
	#pragma warning( disable : 4242 )

	#include <windows.h>
	#include <stdio.h>

	#pragma warning( pop )
#endif

#ifdef USE_SSE
	// Streaming SIMD Extensions Intrinsics include file for 64bit 2-packed 32bit numbers
	#include <mmintrin.h>
	// Streaming SIMD Extensions Intrinsics include file for 128bit 4-packed 32bit numbers
	#include <xmmintrin.h>
	// principal header file for Willamette New Instruction intrinsics
	#include <emmintrin.h>

	//==================================================================
	// following optimization code from:
	// http://www.gamedev.net/community/forums/topic.asp?topic_id=498012
	// author: Moomin from gamedev.net
	//==================================================================

	const _CRT_ALIGN( 16 ) int sign_mask[ 4 ] = { 0x80000000, 0x80000000, 0x80000000, 0x80000000 };
	const _CRT_ALIGN( 16 ) float two_pow_23[ 4 ] = { 8388608.0f, 8388608.0f, 8388608.0f, 8388608.0f };
	const _CRT_ALIGN( 16 ) float one[ 4 ] = { 1.0f, 1.0f, 1.0f, 1.0f };

	/**
	 * @brief
	 * sse2 compatible rounding function
	 */
	inline __m128 _mm_round_ps( __m128 x )
	{
		__m128 t = _mm_or_ps( _mm_and_ps( _mm_load_ps( ( const float* ) sign_mask ), x ), _mm_load_ps( two_pow_23 ) );
		return _mm_sub_ps( _mm_add_ps( x, t ), t );
	}

	/**
	 * @brief
	 * sse2 compatible floor function
	 */
	inline __m128 _mm_floor_ps( __m128 x )
	{
		__m128 t = _mm_round_ps( x );
		return _mm_sub_ps( t, _mm_and_ps( _mm_cmplt_ps( x, t ), _mm_load_ps( one ) ) );
	}
#endif

#endif